﻿@{
    Layout = "~/Views/Shared/_Layout.cshtml";
}
@section styles{
  <style>
      .layui-form-label {
          width:100px;
      }
      .layui-input-block {
          margin-left: 130px;
          min-height: 36px;
      }
     
    .layui-form-required:before {
        content: "*";
        display: inline-block;
        font-family: SimSun;
        margin-right: 4px;
        font-size: 14px;
        line-height: 1;
        color: #ed4014;
    }
    </style>
}

<div class="layui-card">
    <div class="layui-card-body">
        <form class="layui-form" lay-filter="organize-form" style="padding: 15px 0 0 0;">
            <input type="hidden" name="Id" value="@Model.Id" />
            <input type="hidden" id="ParentId" name="ParentId" value="@Model.ParentId" />
            <input type="hidden" id="ProvinceCode" name="ProvinceCode" value="@Model.ProvinceCode" />
            <input type="hidden" id="CityCode" name="CityCode" value="@Model.CityCode" />
            <input type="hidden" id="CountyCode" name="CountyCode" value="@Model.CountyCode" />

              <div class="layui-form-item">
                <label class="layui-form-label layui-form-required">父部门</label>
                 <div class="layui-input-block">
                    <ul id="orgTree" class="dtree" data-id="0"></ul>

                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label layui-form-required">部门名称</label>
                <div class="layui-input-block">
                    <input type="text" name="OrgName" lay-verify="required" maxlength="50" placeholder="请输入部门名称" autocomplete="off" class="layui-input" value="@Model.OrgName">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label layui-form-required">部门编码</label>
                <div class="layui-input-block">
                    <input type="text" name="OrgCode" lay-verify="required" @(Model.Id > 0 ? "readonly" : "") maxlength="80" placeholder="请输入部门编码" autocomplete="off" class="layui-input @(!string.IsNullOrEmpty(Model.OrgCode)?"readonly-b":"")" value="@Model.OrgCode">
                </div>
            </div>
            <div class="layui-form-item layui-hide">
                <label class="layui-form-label layui-form-required">注册公司名称</label>
                <div class="layui-input-block">
                    <input type="text" name="OrgCompanyName"  maxlength="50" placeholder="请输入注册公司名称" autocomplete="off" class="layui-input" value="@Model.OrgCompanyName">
                </div>
            </div>
            <div class="layui-form-item  layui-hide">
                <label class="layui-form-label layui-form-required">法人代表</label>
                <div class="layui-input-block">
                    <input type="text" name="Corporate"  maxlength="50" placeholder="请输入法人代表" autocomplete="off" class="layui-input" value="@Model.Corporate">
                </div>
            </div>
            <div class="layui-form-item">

                <label class="layui-form-label layui-form-required">负责人</label>
                <div class="layui-input-inline">
                    <input type="text" name="Contact"  maxlength="30" placeholder="请输入负责人" autocomplete="off" class="layui-input" value="@Model.Contact">
                </div>
                <label class="layui-form-label layui-form-required">负责人电话</label>
                <div class="layui-input-inline">
                    <input type="text" name="Telephone"  maxlength="20" placeholder="请输入负责人电话" autocomplete="off" class="layui-input" value="@Model.Telephone">
                </div>
                @*<div class="layui-form-mid layui-word-aux">优先填写</div>*@
            </div>
            <div class="layui-form-item layui-hide">
                <label class="layui-form-label">联系人</label>
                <div class="layui-input-inline">
                    <input type="text" name="Contact1" maxlength="30" placeholder="请输入联系人" autocomplete="off" class="layui-input" value="@Model.Contact1">
                </div>
                <label class="layui-form-label ">联系电话</label>
                <div class="layui-input-inline">
                    <input type="text" name="Telephone1" maxlength="20" placeholder="请输入联系电话" autocomplete="off" class="layui-input" value="@Model.Telephone1">
                </div>
            </div>
            <div class="layui-form-item layui-hide">
                <label class="layui-form-label">联系人</label>
                <div class="layui-input-inline">
                    <input type="text" name="Contact2" maxlength="30" placeholder="请输入联系人" autocomplete="off" class="layui-input" value="@Model.Contact2">
                </div>
                <label class="layui-form-label">联系电话</label>
                <div class="layui-input-inline">
                    <input type="text" name="Telephone2" maxlength="20" placeholder="请输入联系电话" autocomplete="off" class="layui-input" value="@Model.Telephone2">
                </div>
            </div>
            <div class="layui-row layui-hide">
                <div class="layui-col-sm8">
                    <div class="layui-form-item" id="area-picker">
                        <label class="layui-form-label layui-form-required">注册地址</label>
                        <div class="layui-input-inline" style="width: 22%;">
                            <select name="province" class="province-selector"  data-value="@Model.Province">
                                <option value="">请选择省</option>
                            </select>
                        </div>
                        <div class="layui-input-inline" style="width: 22%;">
                            <select name="city" class="city-selector"  data-value="@Model.City">
                                <option value="">请选择市</option>
                            </select>
                        </div>
                        <div class="layui-input-inline" style="width: 22%;">
                            <select name="county" class="county-selector" data-value="@Model.County">
                                <option value="">请选择区</option>
                            </select>
                        </div>
                    </div>
                </div>
                <div class="layui-col-sm4">
                    <div class="layui-form-item">
                        <input type="text" name="Addr"  placeholder="请输入注册地址"
                               autocomplete="off" class="layui-input" value="@Model.Addr" maxlength="100" stye="width:100%;margin-left:3%;" />
                    </div>
                </div>
            </div>


            <div class="layui-form-item" style="display:none;">
                <label class="layui-form-label">排序</label>
                <div class="layui-input-block">
                    <input type="number" id="OrgSeq" name="OrgSeq" placeholder="请输入排序"
                           autocomplete="off" class="layui-input" value="@Model.OrgSeq" min="0" max="99" />
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">启用</label>
                <div class="layui-input-block">
                    <input type="checkbox" name="isenable" lay-skin="switch" @(Model.Isenable ? "checked" : "")>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">
                    备注
                </label>
                <div class="layui-input-block">
                    <textarea name="remark" placeholder="请输入描述" class="layui-textarea" style="width:95%;">@Model.Remark</textarea>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">附件列表</label>
                <div class="layui-input-block">
                    <div class="layui-upload">
                        <button type="button" class="pear-btn pear-btn-md" id="multiplefile"><i class="icon pear-icon pear-icon-upload"></i>添加附件</button>
                        <label style="color:orangered;">附件可以是 office、图片、视频(mp4格式)等。</label>
                        <div>
                            <table class="layui-table" id="upload_file_list">
                                <colgroup>
                                    <col>
                                    <col width="120">
                                </colgroup>
                                <thead>
                                    <tr>
                                        <th>名称</th>
                                        <th>操作</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    @if (ViewBag.FileList != null)
                                    {
                                        foreach (var item in ViewBag.FileList)
                                        {
                                            <tr>
                                                <td>
                                                    <input type="hidden" value="@item["id"]" />
                                                    <a href="@item["fpath"]" target="_blank">@item["fname"]</a>
                                                </td>
                                                <td><span class="layui-badge trdel">删除</span></td>
                                            </tr>
                                        }
                                    }
                                </tbody>
                            </table>
                        </div>
                        <div style="width: 120px;margin-left:30px;display:none;" id="divuploadprogress">
                            <div class="layui-progress layui-progress-big" lay-showpercent="yes" lay-filter="uploadprogress">
                                <div class="layui-progress-bar" lay-percent=""></div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn layui-hide" lay-submit lay-filter="organize-submit" id="organize-submit">提交</button>
                </div>
            </div>
        </form>
    </div>
</div>



@section scripts{

    <script type="text/javascript">
        layui.use(['lscom', 'form', 'area', 'element','upload','dtree'], function () {
            var $ = layui.$;
            var lscom = layui.lscom;
            var form = layui.form;
            var element = layui.element;
            var area = layui.area;
            var upload = layui.upload;
            var dtree = layui.dtree;
            var index = parent.layer.getFrameIndex(window.name);
            //parent.layer.iframeAuto(index);

            dtree.render({
                elem: "#orgTree",
                initLevel: "1",
                method: 'get',
                url: "/Systems/Organize/GetdtreeData?id=@Model.Id",
                icon: ["0", "-1"],
                response: { message: "msg", statusCode: 0 },
                dataStyle: "layuiStyle",  //使用layui风格的数据格式
                dataFormat: "list",  //配置data的风格为list
                select: true,
                width:"300",
                selectInitVal: "@Model.ParentId", 
                selectTips:'请选择父级部门'
            });

            // 绑定节点点击
            dtree.on("node('orgTree')", function (obj) {
                $("#ParentId").val(obj.param.nodeId);
            });

            area.render({
                elem: '#area-picker',
                change: function (res) {
                    //选择结果
                    $("#ProvinceCode").val(res.provinceCode);
                    $("#CityCode").val(res.cityCode);
                    $("#CountyCode").val(res.countyCode);
                }
            });

            $(document).on('click', '.trdel', function () {
                $(this).parents('tr').remove();
            })

            upload.render({
                elem: '#multiplefile'
                , url: '/Upload/UploadFile?category=organize' //此处配置你自己的上传接口即可
                , multiple: false
                , accept: 'file'
                , before: function (obj) {
                    $('#divuploadprogress').show();
                }
                , done: function (res) {
                    //上传完毕
                    var htmltr = '<tr>';
                    htmltr += '<td> <input type="hidden" value="' + res.data.id + '" /><a href="' + res.data.url + '"  target="_blank">' + res.data.filename + '</a></td>';
                    htmltr += '<td><span class="layui-badge trdel">删除</span></td>';
                    htmltr += '</tr>';

                    $('#upload_file_list tbody').append(htmltr);
                }
                //进度条
                , progress: function (n, elem, e) {
                    element.progress('uploadprogress', n + '%'); //可配合 layui 进度条元素使用
                    if (n == 100) {
                        //parent.layer.msg('上传完毕', { icon: 1 });
                        $('#divuploadprogress').hide();
                    }
                }
            });
            window.submitForm = function () {
                $("#organize-submit").click();
            }

            form.on('submit(organize-submit)', function (data) {
                var fileids = [];
                $('#upload_file_list input[type="hidden"]').each(function () {
                    fileids.push($(this).val());
                });

                //if (fileids.length < 3) {
                //    parent.layer.msg("产品图片不少于三张", { icon: 5 });
                //    return false;
                //}
                data.field.FileIDs = fileids.join(',');
                data.field.isenable = data.field.isenable == "on";
                if(data.field.ParentId==""){
                    data.field.ParentId = 0;
                }
                var field = data.field;
                var waitIndex = parent.layer.load(2);
                lscom.ajax({
                    url: "/Systems/Organize/SaveOrgRole",
                    data: JSON.stringify(field),
                    lsHandleError: false
                }).done(function (data) {
                    if (data.code == 0) {
                        data.index = index;
                        let parentWindow = parent.selectedWindow().window;
                        parentWindow.saveCallback(data);
                    }
                }).fail(function (message) {
                    lscom.notify.error(message);
                }).always(function () {
                    parent.layer.close(waitIndex);
                });

                return false;
            });
        });
    </script>
}
